<template>
  <div v-if="visible">
    <el-dialog :visible.sync="visible"
               title=""
               :modal="false"
               class="user-dialog add-or-updata"
               width="30%"
               :before-close="closeDialog"
               :close-on-click-modal="false"
               :close-on-press-escape="false">
      <el-form :model="dataForm"
               class="label-left-form"
               v-loading="dataListLoading"
               style="overflow: hidden;"
               :element-loading-text="$t('loading')"
               ref="dataForm"
               label-position="left"
               @keyup.enter.native="dataFormSubmitHandle()"
               label-width="100px">
        <el-form-item prop="sonName"
                      label="系统模块">
          <el-input v-model.trim="dataForm.sonName"
                    disabled
                    placeholder=""></el-input>
        </el-form-item>
        <el-form-item prop="moudleName"
                      label="触发模块">
          <el-input v-model.trim="dataForm.moudleName"
                    disabled
                    placeholder=""></el-input>
        </el-form-item>
        <el-form-item prop="state"
                      label="说明">
          <el-input v-model.trim="dataForm.state"
                    disabled
                    placeholder=""></el-input>
        </el-form-item>
        <el-form-item prop="manufacturers"
                      label="厂家"
                      :rules="required()">
          <el-select v-model="dataForm.manufacturers"
                     placeholder=""
                     @change="changeWay">
            <el-option v-for="item in auditOptions"
                       :key="item.manufacturers"
                       :label="item.name"
                       :value="item.manufacturers">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item prop="thirdPlatformId"
                      label="平台名称"
                      :rules="required()">
          <el-select v-model="dataForm.thirdPlatformId"
                     placeholder="">
            <el-option v-for="item in nameList"
                       :key="item.id"
                       :label="item.platformName"
                       :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item prop="switching"
                      label="开关"
                      :rules="required()">
          <el-radio-group v-model="dataForm.switching">
            <el-radio :label="1">开</el-radio>
            <el-radio :label="0">关</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item prop="templateId"
                      label="短信模板ID"
                      :rules="stringRule()">
          <el-input v-model.trim="dataForm.templateId"
                    placeholder=""></el-input>
        </el-form-item>
        <el-form-item prop="signName"
                      label="签名"
                      :rules="stringRule()">
          <el-input v-model.trim="dataForm.signName"
                    placeholder=""></el-input>
        </el-form-item>
      </el-form>
      <template slot="footer">
        <el-button @click="closeDialog()">{{ $t('cancel') }}</el-button>
        <el-button type="primary"
                   :loading="dataListLoading"
                   @click="dataFormSubmitHandle()">{{ $t('confirm') }}</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import debounce from 'lodash/debounce'
import mixinViewModule from '@/mixins/view-module'
import minFormValidate from '@/mixins/form-validate'
export default {
  mixins: [mixinViewModule, minFormValidate],
  data () {
    return {
      mixinViewModuleOptions: {
        updateURL: '/tb/smsTemplate/updateSmsTemplate'
      },
      visible: false,
      nameList: [],
      auditOptions: [],
      manufacturers: 0,
      dataForm: {
        sonName: '',
        id: '',
        moudleName: '',
        explain: '',
        manufacturers: '',
        switching: 1,
        thirdPlatformId: '',
        templateId: '',
        signName: ''
      }
    }
  },
  methods: {
    closeDialog () {
      this.visible = false
      this.$refs['dataForm'].resetFields()
    },
    init (data) {
      this.dataForm.id = data.id
      if (data.sonName === '0') {
        this.dataForm.sonName = '会务管理系统'
      }
      if (data.sonName === '1') {
        this.dataForm.sonName = '访客管理系统'
      }
      if (data.sonName === '2') {
        this.dataForm.sonName = '车辆管理系统'
      }
      if (data.sonName === '3') {
        this.dataForm.sonName = '宿舍管理系统'
      }
      if (data.sonName === '4') {
        this.dataForm.sonName = '门禁管理系统'
      }
      if (data.sonName === '5') {
        this.dataForm.sonName = '健身房管理系统'
      }
      if (data.sonName === '6') {
        this.dataForm.sonName = '物流车管理系统'
      }
      if (data.sonName === '7') {
        this.dataForm.sonName = '登录系统'
      }
      if (data.moudleName === '0') {
        this.dataForm.moudleName = '会议室预约'
      }
      if (data.moudleName === '1') {
        this.dataForm.moudleName = '访客申请'
      }
      if (data.moudleName === '2') {
        this.dataForm.moudleName = '用车申请'
      }
      if (data.moudleName === '3') {
        this.dataForm.moudleName = '物流车来访申请'
      }
      if (data.moudleName === '4') {
        this.dataForm.moudleName = '员工入住申请'
      }
      if (data.moudleName === '5') {
        this.dataForm.moudleName = '员工调宿申请'
      }
      if (data.moudleName === '6') {
        this.dataForm.moudleName = '临时入住登记'
      }
      if (data.moudleName === '7') {
        this.dataForm.moudleName = '门禁申请'
      }
      if (data.moudleName === '8') {
        this.dataForm.moudleName = '健身房申请'
      }
      if (data.moudleName === '9') {
        this.dataForm.moudleName = '物流车申请'
      }
      if (data.moudleName === '11') {
        this.dataForm.moudleName = '忘记密码'
      }
      this.dataForm.manufacturers = data.manufacturers
      this.dataForm.switching = data.switching
      this.dataForm.templateId = data.templateId
      this.dataForm.signName = data.signName
      this.visible = true
      this.changeWay(this.dataForm.manufacturers)
      this.getManufacturers()
      this.dataForm.thirdPlatformId = data.thirdPlatformId
      console.log(this.dataForm.thirdPlatformId)
    },
    getManufacturers () {
      this.$http.get(`/tb/thirdPlatform/queryList?type=3`).then(({ data: res }) => {
        if (res.code !== 0) {
          return
        }
        res.data.map(item => {
          if (item.manufacturers === 5) {
            item.name = '阿里云'
          }
        })
        this.auditOptions = res.data
      })
    },
    changeWay (val) {
      if (this.manufacturers !== val) {
        this.dataForm.thirdPlatformId = ''
      }
      this.manufacturers = val
      this.$http.get(`/tb/thirdPlatform/queryList?manufacturers=${this.manufacturers}`).then(({ data: res }) => {
        if (res.code !== 0) {
          return
        }
        this.nameList = res.data
      })
    },
    dataFormSubmitHandle: debounce(function () {
      this.$refs.dataForm.validate((valid) => {
        if (valid) {
          this.$http.put(this.mixinViewModuleOptions.updateURL, {
            id: this.dataForm.id,
            // manufacturers: this.dataForm.manufacturers,
            switching: this.dataForm.switching,
            templateId: this.dataForm.templateId,
            thirdPlatformId: this.dataForm.thirdPlatformId,
            signName: this.dataForm.signName
          }).then(({ data: res }) => {
            if (res.code !== 0) {
              return
            }
            this.$message.success('操作成功')
            this.closeDialog()
            this.$emit('callBack')
          })
        }
      })
    }, 1000, { 'leading': true, 'trailing': false })
  }
}
</script>

<style lang="scss" scoped>
::deep .el-select {
  width: 100%;
}
</style>
